package com.wxq.gps;

import java.util.Map;

import com.wxq.draw.DrawDBTool;
import com.wxq.schoolmap.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SpotGPSActivity extends Activity {

	EditText jingET;
	EditText weiET;
	
	Button startBtn;
	Button stopBtn;
	Button submitBtn;
	
	LocationManager locmgr;
	LocationListener locListener;
	
	String spotId;
	String MallDBpath;
	DrawDBTool dbTool;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spot_gps);
		
		Intent intent = getIntent();
		spotId = intent.getStringExtra("spotId");
		MallDBpath = intent.getStringExtra("MallDBpath");
		dbTool = new DrawDBTool(this);
		dbTool.setDBpath(MallDBpath);
		
		setupAllViews();
		
		Map<String, String> map = dbTool.getCoordFromOutdoor(spotId);
		jingET.setText(map.get("Longitude"));
		weiET.setText(map.get("Latitude"));
		
		if (locmgr == null) {
			locmgr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		}
		
		locListener = new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO 自动生成的方法存根
				weiET.setText(location.getLatitude()+"");
				jingET.setText(location.getLongitude()+"");
			}
		};
		
		startBtn.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v) {
				// 开启GPS
				openGPSSettings();
			}});
		
		stopBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 取消GPS定位
				locmgr.removeUpdates(locListener);
			}
		});
		
		submitBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 提交GPS坐标
				dbTool.changeCoord2Outdoor(
						spotId, 
						Double.parseDouble(weiET.getText().toString()), 
						Double.parseDouble(jingET.getText().toString()));
			}
		});
	}
	
	private void setupAllViews() {
		jingET = (EditText) findViewById(R.id.jingET);
		weiET = (EditText) findViewById(R.id.weiET);
		startBtn = (Button) findViewById(R.id.startGPS);
		stopBtn = (Button) findViewById(R.id.stopGPS);
		submitBtn = (Button) findViewById(R.id.submitBtn);
	}

	private void openGPSSettings() {
        if (locmgr.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
            Log.d("wxq", "GPS模块正常！");
            Toast.makeText(this, "正在定位,请稍等...", Toast.LENGTH_SHORT).show();
            locmgr.requestLocationUpdates(
            		LocationManager.GPS_PROVIDER, 
            		0, 
            		0, 
            		locListener);
            return;
        }

        Toast.makeText(this, "请开启GPS！", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivityForResult(intent,0); //此为设置完成后返回到获取界面

    }
	
	@Override
	protected void onDestroy() {
		dbTool.close();
		super.onDestroy();
	}
	
}
